﻿#Region "License & Usage"

'Microsoft Public License (Ms-PL) 

'This license governs use of the accompanying software. If you use the 
'software, you accept this license. If you do not accept the license, do 
'not use the software. 

'1. Definitions 

'The terms "reproduce," "reproduction," "derivative works," and 
'"distribution" have the same meaning here as under U.S. copyright law. 

'A "contribution" is the original software, or any additions or changes 
'to the software. 

'A "contributor" is any person that distributes its contribution under 
'this license. 

'"Licensed patents" are a contributor's patent claims that read directly 
'on its contribution. 

'2. Grant of Rights 

'(A) Copyright Grant- Subject to the terms of this license, including the 
'license conditions and limitations in section 3, each contributor grants 
'you a non-exclusive, worldwide, royalty-free copyright license to 
'reproduce its contribution, prepare derivative works of its 
'contribution, and distribute its contribution or any derivative works 
'that you create. 

'(B) Patent Grant- Subject to the terms of this license, including the 
'license conditions and limitations in section 3, each contributor grants 
'you a non-exclusive, worldwide, royalty-free license under its licensed 
'patents to make, have made, use, sell, offer for sale, import, and/or 
'otherwise dispose of its contribution in the software or derivative 
'works of the contribution in the software. 

'3. Conditions and Limitations 

'(A) No Trademark License- This license does not grant you rights to use 
'any contributors' name, logo, or trademarks. 

'(B) If you bring a patent claim against any contributor over patents 
'that you claim are infringed by the software, your patent license from 
'such contributor to the software ends automatically. 

'(C) If you distribute any portion of the software, you must retain all 
'copyright, patent, trademark, and attribution notices that are present 
'in the software. 

'(D) If you distribute any portion of the software in source code form, 
'you may do so only under this license by including a complete copy of 
'this license with your distribution. If you distribute any portion of 
'the software in compiled or object code form, you may only do so under a 
'license that complies with this license. 

'(E) The software is licensed "as-is." You bear the risk of using it. The 
'contributors give no express warranties, guarantees or conditions. You 
'may have additional consumer rights under your local laws which this 
'license cannot change. To the extent permitted under your local laws, 
'the contributors exclude the implied warranties of merchantability, 
'fitness for a particular purpose and non-infringement.

#End Region

#Region "Options"

Option Strict On
Option Explicit On

#End Region

#Region "Imports"

'From .Net Framework 3.5
Imports Microsoft.Drawing
Imports System.Drawing
Imports System.IO
Imports System.Windows.Forms

'From Custom Classes/Reference
Imports StedySoft.SenseSDK
Imports StedySoft.SenseSDK.DrawingCE
Imports SenseSDKExtended.DrawingCEEx
Imports SenseSDKExtended

#End Region

Namespace SenseHeaderControlEx

    ''' <summary>SenseHeaderControlEx.HTCContactsStyle1</summary>
    ''' <author>Greenhacks</author>
    ''' <author-reference>None</author-reference>
    ''' <copyright>None</copyright>
    ''' <version>0.1</version>
    ''' <remarks>None</remarks>
    ''' <reference>None</reference>
    Public Class HTCContactsStyle1

#Region "Fields"

        'General Properties
        Private _backColor As System.Drawing.Color = Nothing

        'PrimaryText Properties
        Private _Text As String = Nothing
        Private _TextRectangle As Rectangle = Nothing
        Private _TextFontName As String = "Tahoma" '"Helvetica Neue OTS"
        Private _TextFontSize As Single = 9.0F
        Private _TextFontStyle As System.Drawing.FontStyle = FontStyle.Regular
        Private _TextForeColor As System.Drawing.Color = Color.Black
        Private _TextBackColor As System.Drawing.Color = Color.Gray
        Private _TextAlignment As System.Drawing.StringAlignment = StringAlignment.Near

        'Hold Images
        Private _iThumbnailLeft As Microsoft.Drawing.IImage = Nothing
        Private _iThumbnailRight As Microsoft.Drawing.IImage = Nothing

        'Hold Cordinates
        Private _iThumbnailLeftRectangle As Rectangle = Nothing
        Private _iThumbnailRightRectangle As Rectangle = Nothing

        'Hold Margins
        Private _iThumbnailLeftMarginLR As Double = 0
        Private _iThumbnailLeftMarginTB As Double = 0
        Private _iThumbnailRightMarginLR As Double = 0
        Private _iThumbnailRightMarginTB As Double = 0

        Private path_header_background As IImage = _getIImageFromResource("header.style1")

#End Region

#Region "Events"

        ''' <summary>
        ''' OnClick Event
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <remarks>General Control OnClick Event</remarks>
        Public Shadows Event OnClick(ByVal sender As Object)

        ''' <summary>
        ''' OnClickiThumbnailLeft Event
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <remarks></remarks>
        Public Event OnClickiThumbnailLeft(ByVal sender As Object)

        ''' <summary>
        ''' OnClickiThumbnailRight Event
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <remarks></remarks>
        Public Event OnClickiThumbnailRight(ByVal sender As Object)

        ''' <summary>
        ''' OnClickText Event
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <remarks></remarks>
        Public Event OnClickText(ByVal sender As Object)

#End Region

#Region "Methods"

        ''' <summary>
        ''' _getIImageFromResource
        ''' </summary>
        ''' <param name="resource"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Private Function _getIImageFromResource(ByVal resource As String) As IImage
            Dim iimg As IImage = Nothing
            Using strm As MemoryStream = If(_isVGA, SenseSDKExtended.Resources.VGA.Images.GetIImageFromResource(resource), SenseSDKExtended.Resources.QVGA.Images.GetIImageFromResource(resource))
                ImagingFactory.GetImaging.CreateImageFromBuffer(strm.GetBuffer, Convert.ToUInt32(strm.Length), BufferDisposalFlag.BufferDisposalFlagNone, iimg)
            End Using
            Return iimg
        End Function

        ''' <summary>
        ''' Check if it is VGA or QVGA
        ''' </summary>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Private Function _isVGA() As Boolean

            Try

                'Return the output
                Return StedySoft.SenseSDK.DrawingCE.Resolution.ScreenIsVGA

            Catch ex As Exception

                'Throw exception if any other error caught
                Throw New Exception(ex.Message)

            End Try

        End Function

        ''' <summary>
        ''' Draw_HTCContactsStyle1
        ''' </summary>
        ''' <param name="e"></param>
        ''' <remarks></remarks>
        Public Sub Draw_HTCContactsStyle1(ByVal e As System.Windows.Forms.PaintEventArgs)

            Dim _iThumbnailLeftMarginLR As Integer = If(_isVGA, 20, 10)
            Dim _iThumbnailLeftMarginTB As Integer = If(_isVGA, 20, 10)
            Dim _iThumbnailLeftX As Integer = If(_isVGA, 10, 5)
            Dim _iThumbnailLeftY As Integer = If(_isVGA, 10, 5)
            Dim _iThumbnailLeftW As Integer = If(Not _iThumbnailLeft Is Nothing, SenseAPIs.SenseImage.GetIImageSize(_iThumbnailLeft).Width + _iThumbnailLeftMarginLR, If(_isVGA, 10, 5))
            Dim _iThumbnailLeftH As Integer = If(Not _iThumbnailLeft Is Nothing, SenseAPIs.SenseImage.GetIImageSize(_iThumbnailLeft).Height, 0)

            Dim _iThumbnailRightMarginLR As Integer = If(_isVGA, 20, 10)
            Dim _iThumbnailRightMarginTB As Integer = If(_isVGA, 20, 10)
            Dim _iThumbnailRightX As Integer = If(_isVGA, 10, 5)
            Dim _iThumbnailRightY As Integer = If(_isVGA, 10, 5)
            Dim _iThumbnailRightW As Integer = If(Not _iThumbnailRight Is Nothing, SenseAPIs.SenseImage.GetIImageSize(_iThumbnailRight).Width + _iThumbnailRightMarginLR, If(_isVGA, 10, 5))
            Dim _iThumbnailRightH As Integer = If(Not _iThumbnailRight Is Nothing, SenseAPIs.SenseImage.GetIImageSize(_iThumbnailRight).Height, 0)

            Dim _TextMarginTB As Integer = If(_isVGA, 20, 10)
            Dim _TextX As Integer = _iThumbnailLeftW
            Dim _TextY As Integer = If(_isVGA, 10, 5)
            Dim _TextW As Integer = Screen.PrimaryScreen.Bounds.Width - (_iThumbnailLeftW + _iThumbnailRightW)
            Dim _TextH As Integer = SenseAPIsEx.SenseStrings.CalculateStringHeight(Text, TextFontName, CInt(TextFontSize), TextFontStyle, New Size(_TextW, Nothing))

            If Not _iThumbnailLeft Is Nothing Then

                _iThumbnailLeftRectangle = New Rectangle(ClientRectangle.Left + If(_isVGA, 10, 5), CInt((ClientRectangle.Height - SenseAPIs.SenseImage.GetIImageSize(_iThumbnailLeft).Height) / 2), SenseAPIs.SenseImage.GetIImageSize(_iThumbnailLeft).Width, SenseAPIs.SenseImage.GetIImageSize(_iThumbnailLeft).Height)
                _iThumbnailLeftMarginTB = If(_isVGA, 10, 5)
                _iThumbnailLeftMarginLR = If(_isVGA, 10, 5)

                Dim ___iThumbnailLeft As IImage = _iThumbnailLeft
                e.Graphics.DrawImageAlphaChannel(___iThumbnailLeft, New Rectangle(CInt(_iThumbnailLeftRectangle.Left), CInt(_iThumbnailLeftRectangle.Top), _iThumbnailLeftRectangle.Width, _iThumbnailLeftRectangle.Height))

            End If

            If Not _iThumbnailRight Is Nothing Then

                _iThumbnailRightRectangle = New Rectangle(Screen.PrimaryScreen.Bounds.Width - (SenseAPIs.SenseImage.GetIImageSize(_iThumbnailRight).Width + If(_isVGA, 10, 5)), CInt((ClientRectangle.Height - SenseAPIs.SenseImage.GetIImageSize(_iThumbnailRight).Height) / 2), SenseAPIs.SenseImage.GetIImageSize(_iThumbnailRight).Width, SenseAPIs.SenseImage.GetIImageSize(_iThumbnailRight).Height)
                _iThumbnailRightMarginTB = If(_isVGA, 10, 5)
                _iThumbnailRightMarginLR = If(_isVGA, 10, 5)

                Dim ___iThumbnailRight As IImage = _iThumbnailRight
                e.Graphics.DrawImageAlphaChannel(___iThumbnailRight, New Rectangle(CInt(_iThumbnailRightRectangle.Left), CInt(_iThumbnailRightRectangle.Top), _iThumbnailRightRectangle.Width, _iThumbnailRightRectangle.Height))

            End If

            _TextRectangle = New Rectangle(_TextX, _TextY, _TextW, _TextH)

            If _Text Is Nothing Then Exit Sub

            Dim _CharsCanFit_Text As Integer = SenseAPIsEx.SenseStrings.CalculateCharactersCanFit(_Text, _TextFontName, CInt(_TextFontSize), _TextFontStyle, New Size(_TextRectangle.Width, _TextRectangle.Height), e.Graphics)

            Try

                If Not _Text = String.Empty Then
                    'Draw the Text with other properties like height,color,style,alignment,etc
                    Using f As Font = New Font(_TextFontName, _TextFontSize, _TextFontStyle)
                        Dim s As String = If(_Text.Length > _CharsCanFit_Text, _Text.Substring(0, _CharsCanFit_Text) & "...", _Text)
                        Dim r As Rectangle = New Rectangle(_TextRectangle.Left, ClientRectangle.Top + If(_isVGA, 10, 5), _TextRectangle.Width, ClientRectangle.Height - If(_isVGA, 20, 10)) '(Left,Top,Width,Height)
                        Using b As Brush = New SolidBrush(_TextForeColor)
                            Using sf As StringFormat = New StringFormat()
                                sf.Alignment = _TextAlignment
                                sf.LineAlignment = StringAlignment.Center
                                e.Graphics.DrawString(s, f, b, r, sf)
                            End Using
                        End Using
                    End Using
                End If

            Catch ex As Exception

                'Throw exception if any other error caught
                Throw New Exception(ex.Message)

            End Try

        End Sub

        ''' <summary>
        ''' HTCContactsStyle1_MouseUp
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        ''' <remarks></remarks>
        Private Sub HTCContactsStyle1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp

            If e.Y > ClientRectangle.Height Then
                Exit Sub
            Else

                'Raise OnClick Event before other events first
                RaiseEvent OnClick(Me)

                If Not _iThumbnailLeft Is Nothing Then

                    Select Case e.X

                        Case _iThumbnailLeftRectangle.Left To _iThumbnailLeftRectangle.Left + _iThumbnailLeftRectangle.Width

                            'Raise OnClickiThumbnailLeft Event
                            RaiseEvent OnClickiThumbnailLeft(Me)

                    End Select

                End If

                If Not _iThumbnailRight Is Nothing Then

                    Select Case e.X

                        Case _iThumbnailRightRectangle.Left To _iThumbnailRightRectangle.Left + _iThumbnailRightRectangle.Width

                            'Raise OnClickiThumbnailRight Event
                            RaiseEvent OnClickiThumbnailRight(Me)

                    End Select

                End If

                If Not _Text Is Nothing Then

                    Select Case e.X

                        Case _TextRectangle.Left To _TextRectangle.Left + _TextRectangle.Width

                            'Raise OnClickText Event
                            RaiseEvent OnClickText(Me)

                    End Select

                End If

            End If

        End Sub

        ''' <summary>
        ''' HTCContactsStyle1_Paint
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        ''' <remarks></remarks>
        Private Sub HTCContactsStyle1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint

            Try

                Dim ___backgroundImage As IImage = path_header_background
                e.Graphics.DrawImageAlphaChannel(___backgroundImage, New Rectangle(ClientRectangle.Left, ClientRectangle.Top, ClientRectangle.Width, ClientRectangle.Height))

                Draw_HTCContactsStyle1(e)

            Catch ex As Exception

                'Throw exception if any other error caught
                Throw New Exception(ex.Message)

            End Try

        End Sub

#End Region

#Region "Properties"

        ''' <summary>
        ''' Text
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Overrides Property Text() As String
            Get
                Return _Text
            End Get
            Set(ByVal value As String)
                _Text = value
            End Set
        End Property

        ''' <summary>
        ''' TextFontName
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property TextFontName() As String
            Get
                Return _TextFontName
            End Get
            Set(ByVal value As String)
                _TextFontName = value
            End Set
        End Property

        ''' <summary>
        ''' TextFontSize
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property TextFontSize() As Single
            Get
                Return _TextFontSize
            End Get
            Set(ByVal value As Single)
                _TextFontSize = value
            End Set
        End Property

        ''' <summary>
        ''' TextFontStyle
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property TextFontStyle() As System.Drawing.FontStyle
            Get
                Return _TextFontStyle
            End Get
            Set(ByVal value As System.Drawing.FontStyle)
                _TextFontStyle = value
            End Set
        End Property

        ''' <summary>
        ''' TextForeColor
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property TextForeColor() As System.Drawing.Color
            Get
                Return _TextForeColor
            End Get
            Set(ByVal value As System.Drawing.Color)
                _TextForeColor = value
            End Set
        End Property

        ''' <summary>
        ''' TextBackColor
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property TextBackColor() As System.Drawing.Color
            Get
                Return _TextBackColor
            End Get
            Set(ByVal value As System.Drawing.Color)
                _TextBackColor = value
            End Set
        End Property

        ''' <summary>
        ''' TextAlignment
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property TextAlignment() As System.Drawing.StringAlignment
            Get
                Return _TextAlignment
            End Get
            Set(ByVal value As System.Drawing.StringAlignment)
                _TextAlignment = value
            End Set
        End Property

        ''' <summary>
        ''' iThumbnailLeft
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        <CLSCompliant(False)> _
        Public Property iThumbnailLeft() As Microsoft.Drawing.IImage
            Get
                Return _iThumbnailLeft
            End Get
            Set(ByVal value As Microsoft.Drawing.IImage)
                _iThumbnailLeft = value
            End Set
        End Property

        ''' <summary>
        ''' iThumbnailRight
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        <CLSCompliant(False)> _
        Public Property iThumbnailRight() As Microsoft.Drawing.IImage
            Get
                Return _iThumbnailRight
            End Get
            Set(ByVal value As Microsoft.Drawing.IImage)
                _iThumbnailRight = value
            End Set
        End Property

#End Region

    End Class

End Namespace